﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessWords : IBusinessWords
    {
        IDALWords _dalWord;
        IUnitOfWork _unitWork;
        public BusinessWords(IDALWords dalWord, IUnitOfWork unitWork)
        {
            _dalWord = dalWord;
            _unitWork = unitWork;
        }
        public void Add(WordsModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Words>(model);
            dataEntity.CreateTime = DateTime.Now;
            _dalWord.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(WordsModel model)
        {
            var dataEntity = _dalWord.Get(model.ID);
            dataEntity.Content = model.Content;
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<WordsModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Words>>(models);
            _dalWord.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<WordsModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Words>>(models);
            _dalWord.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<WordsModel> GetAll(bool isTracking = true)
        {
            var entities = _dalWord.GetAll(isTracking);
            return Mapper.Map<List<WordsModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalWord.Delete(primaryKey);
            _unitWork.Commit();
        }

        public WordsModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalWord.Get(primaryKey);
            return Mapper.Map<WordsModel>(dataEntity);
        }

        public void Delete(WordsModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Words>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalWord.Delete(where);
            _unitWork.Commit();
        }

        public WordsModel Get(WordsModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Words>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.UserID))
            {
                where = where.And(x => x.UserID.ToLower() == model.UserID.ToLower());
            }
            var dataEntity = _dalWord.Get(where);
            return Mapper.Map<WordsModel>(dataEntity);
        }

        public List<WordsModel> GetAll(WordsModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Words>();
            if (!string.IsNullOrWhiteSpace(model.UserID))
            {
                where = where.And(x => x.UserID.ToLower() == model.UserID.ToLower());
            }
            var entities = _dalWord.GetAll(where, isTracking);
            return Mapper.Map<List<WordsModel>>(entities);
        }


        public List<WordsModel> GetAll(WordsModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }

        public void AddAgree(int id, string account)
        {
            var dataEntity = _dalWord.Get(id);
            dataEntity.AgreeUsers = string.IsNullOrWhiteSpace(dataEntity.AgreeUsers) ? account : dataEntity.AgreeUsers + "&" + account;
            _unitWork.Commit();
        }
    }
}
